package org.htmlparser.sax;

import java.io.IOException;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.Remark;
import org.htmlparser.Tag;
import org.htmlparser.Text;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.util.DefaultParserFeedback;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.ParserFeedback;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.NamespaceSupport;

public class XMLReader
  implements org.xml.sax.XMLReader
{
  protected ContentHandler mContentHandler;
  protected DTDHandler mDTDHandler;
  protected EntityResolver mEntityResolver;
  protected ErrorHandler mErrorHandler;
  protected boolean mNameSpacePrefixes;
  protected boolean mNameSpaces = true;
  protected Parser mParser;
  protected String[] mParts;
  protected NamespaceSupport mSupport;

  public XMLReader()
  {
    this.mNameSpacePrefixes = false;
    this.mEntityResolver = null;
    this.mDTDHandler = null;
    this.mContentHandler = null;
    this.mErrorHandler = null;
    this.mSupport = new NamespaceSupport();
    this.mSupport.pushContext();
    this.mSupport.declarePrefix("", "http://www.w3.org/TR/REC-html40");
    this.mParts = new String[3];
  }

  protected void doSAX(Node paramNode)
    throws ParserException, SAXException
  {
    label270: label303: Tag localTag2;
    if (paramNode instanceof Remark)
    {
      String str2 = this.mParser.getLexer().getPage().getText(paramNode.getStartPosition(), paramNode.getEndPosition());
      this.mContentHandler.ignorableWhitespace(str2.toCharArray(), 0, str2.length());
    }
    do
    {
      do
        while (true)
        {
          return;
          if (!(paramNode instanceof Text))
            break;
          String str1 = this.mParser.getLexer().getPage().getText(paramNode.getStartPosition(), paramNode.getEndPosition());
          this.mContentHandler.characters(str1.toCharArray(), 0, str1.length());
        }
      while (!(paramNode instanceof Tag));
      Tag localTag1 = (Tag)paramNode;
      if (this.mNameSpaces)
      {
        this.mSupport.processName(localTag1.getTagName(), this.mParts, false);
        if (!(this.mNameSpacePrefixes))
          break label270;
        this.mParts[2] = localTag1.getTagName();
      }
      while (true)
      {
        while (true)
        {
          while (true)
          {
            this.mContentHandler.startElement(this.mParts[0], this.mParts[1], this.mParts[2], new Attributes(localTag1, this.mSupport, this.mParts));
            NodeList localNodeList = localTag1.getChildren();
            if (localNodeList == null)
              break label303;
            int i = 0;
            while (true)
            {
              if (i >= localNodeList.size())
                break label303;
              doSAX(localNodeList.elementAt(i));
              ++i;
            }
            this.mParts[0] = "";
            this.mParts[1] = "";
          }
          if (!(this.mNameSpaces))
            break;
          this.mParts[2] = "";
        }
        this.mParts[2] = localTag1.getTagName();
      }
      localTag2 = localTag1.getEndTag();
    }
    while (localTag2 == null);
    if (this.mNameSpaces)
    {
      this.mSupport.processName(localTag2.getTagName(), this.mParts, false);
      if (!(this.mNameSpacePrefixes))
        break label412;
      this.mParts[2] = localTag2.getTagName();
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
            this.mContentHandler.endElement(this.mParts[0], this.mParts[1], this.mParts[2]);
          this.mParts[0] = "";
          this.mParts[1] = "";
        }
        label412: if (!(this.mNameSpaces))
          break;
        this.mParts[2] = "";
      }
      this.mParts[2] = localTag2.getTagName();
    }
  }

  public ContentHandler getContentHandler()
  {
    return this.mContentHandler;
  }

  public DTDHandler getDTDHandler()
  {
    return this.mDTDHandler;
  }

  public EntityResolver getEntityResolver()
  {
    return this.mEntityResolver;
  }

  public ErrorHandler getErrorHandler()
  {
    return this.mErrorHandler;
  }

  public boolean getFeature(String paramString)
    throws SAXNotRecognizedException, SAXNotSupportedException
  {
    boolean bool;
    if (paramString.equals("http://xml.org/sax/features/namespaces"))
      bool = this.mNameSpaces;
    while (true)
    {
      return bool;
      if (!(paramString.equals("http://xml.org/sax/features/namespace-prefixes")))
        break;
      bool = this.mNameSpacePrefixes;
    }
    throw new SAXNotSupportedException(paramString + " not yet understood");
  }

  public Object getProperty(String paramString)
    throws SAXNotRecognizedException, SAXNotSupportedException
  {
    throw new SAXNotSupportedException(paramString + " not yet understood");
  }

  public void parse(String paramString)
    throws IOException, SAXException
  {
    if (this.mContentHandler != null)
      try
      {
        Object localObject;
        this.mParser = new Parser(paramString);
        Locator localLocator = new Locator(this.mParser);
        if (this.mErrorHandler != null)
        {
          localObject = new Feedback(this.mErrorHandler, localLocator);
          this.mParser.setFeedback((ParserFeedback)localObject);
          this.mContentHandler.setDocumentLocator(localLocator);
        }
        try
        {
          this.mContentHandler.startDocument();
          NodeIterator localNodeIterator = this.mParser.elements();
          if (localNodeIterator.hasMoreNodes())
            doSAX(localNodeIterator.nextNode());
        }
        catch (SAXException localSAXException)
        {
          while (true)
          {
            if (this.mErrorHandler == null)
              return;
            this.mErrorHandler.fatalError(new SAXParseException("contentHandler threw me", localLocator, localSAXException));
            return;
            localObject = new DefaultParserFeedback(0);
          }
          this.mContentHandler.endDocument();
        }
      }
      catch (ParserException localParserException)
      {
        if (this.mErrorHandler != null)
          this.mErrorHandler.fatalError(new SAXParseException(localParserException.getMessage(), "", paramString, 0, 0));
      }
  }

  public void parse(InputSource paramInputSource)
    throws IOException, SAXException
  {
    if (this.mContentHandler != null)
      try
      {
        Object localObject;
        this.mParser = new Parser(new Lexer(new Page(paramInputSource.getByteStream(), paramInputSource.getEncoding())));
        Locator localLocator = new Locator(this.mParser);
        if (this.mErrorHandler != null)
        {
          localObject = new Feedback(this.mErrorHandler, localLocator);
          this.mParser.setFeedback((ParserFeedback)localObject);
          this.mContentHandler.setDocumentLocator(localLocator);
        }
        try
        {
          this.mContentHandler.startDocument();
          NodeIterator localNodeIterator = this.mParser.elements();
          if (localNodeIterator.hasMoreNodes())
            doSAX(localNodeIterator.nextNode());
        }
        catch (SAXException localSAXException)
        {
          while (true)
          {
            if (this.mErrorHandler == null)
              return;
            this.mErrorHandler.fatalError(new SAXParseException("contentHandler threw me", localLocator, localSAXException));
            return;
            localObject = new DefaultParserFeedback(0);
          }
          this.mContentHandler.endDocument();
        }
      }
      catch (ParserException localParserException)
      {
        if (this.mErrorHandler != null)
          this.mErrorHandler.fatalError(new SAXParseException(localParserException.getMessage(), "", "", 0, 0));
      }
  }

  public void setContentHandler(ContentHandler paramContentHandler)
  {
    this.mContentHandler = paramContentHandler;
  }

  public void setDTDHandler(DTDHandler paramDTDHandler)
  {
    this.mDTDHandler = paramDTDHandler;
  }

  public void setEntityResolver(EntityResolver paramEntityResolver)
  {
    this.mEntityResolver = paramEntityResolver;
  }

  public void setErrorHandler(ErrorHandler paramErrorHandler)
  {
    this.mErrorHandler = paramErrorHandler;
  }

  public void setFeature(String paramString, boolean paramBoolean)
    throws SAXNotRecognizedException, SAXNotSupportedException
  {
    if (paramString.equals("http://xml.org/sax/features/namespaces"))
      this.mNameSpaces = paramBoolean;
    while (true)
    {
      return;
      if (!(paramString.equals("http://xml.org/sax/features/namespace-prefixes")))
        break;
      this.mNameSpacePrefixes = paramBoolean;
    }
    throw new SAXNotSupportedException(paramString + " not yet understood");
  }

  public void setProperty(String paramString, Object paramObject)
    throws SAXNotRecognizedException, SAXNotSupportedException
  {
    throw new SAXNotSupportedException(paramString + " not yet understood");
  }
}